<template>
  <div class="app-container">
    <!-- 添加或修改新闻对话框 -->
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="新闻id" prop="newsId" v-if="!isSH || !isUpdate">
          <el-input v-model="form.newsId" placeholder="请输入新闻id" :disabled="isUpdate"/>
        </el-form-item>
        <el-form-item label="新闻标题" prop="newsTitle" v-if="!isSH || !isUpdate">
          <el-input v-model="form.newsTitle" placeholder="请输入新闻标题" />
        </el-form-item>
        <el-form-item label="新闻内容" v-if="!isSH || !isUpdate">
          <editor v-model="form.newsContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="发布时间" prop="newsTime" v-if="!isSH || !isUpdate">
          <el-date-picker clearable
            v-model="form.newsTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择新闻发布时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="用户" prop="uid" v-if="!isSH || !isUpdate">
          <el-select v-model="form.uid" placeholder="请选择用户" clearable :disabled="isUpdate">
            <el-option
              v-for="user in userList"
              :key="user.value"
              :label="user.uname"
              :value="user.uid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="status" v-if="isSH">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.zhhq_sh"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark" v-if="isSH">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
  </div>
</template>

<script>
import { listNews, getNews, delNews, addNews, updateNews } from "@/api/zhhq/news";
import { listUser } from '@/api/zhhq/user'

export default {
  name: "News.add",
  dicts: ['zhhq_sh'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //当前操作是修改？
      isUpdate:false,
      // 新闻表格数据
      newsList: [],
      userList: [],
      isSH: false,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        newsTitle: null,
        newsContent: null,
        newsTime: null,
        uid: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        newsId: [
          { required: true, message: "新闻id不能为空", trigger: "blur" }
        ],
        newsTitle: [
          { required: true, message: "新闻标题不能为空", trigger: "blur" }
        ],
        newsContent: [
          { required: true, message: "新闻内容不能为空", trigger: "blur" }
        ],
        newsTime: [
          { required: true, message: "新闻发布时间不能为空", trigger: "blur" }
        ],
        uid: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getInfo();
  },
  methods: {
    /** 查询新闻列表 */
    getList() {
      this.loading = true;
      listNews(this.queryParams).then(response => {
        this.newsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listUser().then(response => {
        this.userList = response.rows
      })
    },
    getInfo(){
    const newsId =  this.$route.params && this.$route.params.newsId
    getNews(newsId).then(response => {
    this.form = response.data;
    });
    },
    // 取消按钮
    cancel() {
      this.$router.go(-1);
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        newsId: null,
        newsTitle: null,
        newsContent: null,
        newsTime: null,
        uid: null,
        status: "1",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handlePJ(row){
    this.$router.push({
    path: `/zhkf/appraiseid-d/index/${row}`
    });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.newsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.isUpdate = false
      this.isSH = false
      this.title = "添加新闻";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const newsId = row.newsId || this.ids
      getNews(newsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改新闻";
        this.isUpdate=true;
        this.isSH = false
      });
    },
    handleSH(row) {
      this.reset();
      const newsId = row.newsId || this.ids
      getNews(newsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "审核新闻";
        this.isUpdate=true;
        this.isSH = true
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.isUpdate) {
            updateNews(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNews(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const newsIds = row.newsId || this.ids;
      this.$modal.confirm('是否确认删除新闻编号为"' + newsIds + '"的数据项？').then(function() {
        return delNews(newsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('zhhq/news/export', {
        ...this.queryParams
      }, `news_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
